package com.imesne.office.excel.write;

import com.imesne.office.excel.model.ExcelRange;
import com.imesne.office.excel.model.ExcelVersion;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liyd on 17/7/4.
 */
public class ExcelWriterConfig {

    private ExcelBookWriter excelBookWriter;

    private ExcelSheetWriter excelSheetWriter;

    private ExcelRowWriter excelRowWriter;

    private ExcelCellWriter excelCellWriter;

    /**
     * 版本 xlsx xls
     */
    private ExcelVersion version;

    /**
     * 是否追加数据
     */
    private boolean isAppend;

    /**
     * 是否克隆sheet模板
     */
    private boolean isCloneSheetTemplate;

    /**
     * 合并的单元格
     */
    private Map<Integer, List<ExcelRange>> mergedRegions;

    /**
     * 底部固定页脚
     */
    private Map<Integer, ExcelRange> footers;

    private ExcelWriteProcessor excelWriteProcessor;

    public ExcelWriterConfig() {
        this.excelBookWriter = new ExcelBookWriterImpl();
        this.excelSheetWriter = new ExcelSheetWriterImpl();
        this.excelRowWriter = new ExcelRowWriterImpl();
        this.excelCellWriter = new ExcelCellWriterImpl();
        this.version = ExcelVersion.XLSX;
        this.isAppend = false;
        this.isCloneSheetTemplate = false;
        mergedRegions = new HashMap<>();
        footers = new HashMap<>();
    }

    private void initReader(Object reader) {
        if (reader instanceof AbstractConfigWriter) {
            ((AbstractConfigWriter) reader).setExcelWriterConfig(this);
        }
    }

    public ExcelBookWriter getExcelBookWriter() {
        initReader(excelBookWriter);
        return excelBookWriter;
    }

    public ExcelSheetWriter getExcelSheetWriter() {
        initReader(excelSheetWriter);
        return excelSheetWriter;
    }

    public ExcelRowWriter getExcelRowWriter() {
        initReader(excelRowWriter);
        return excelRowWriter;
    }

    public ExcelCellWriter getExcelCellWriter() {
        initReader(excelCellWriter);
        return excelCellWriter;
    }

    public boolean isAppend() {
        return isAppend;
    }

    public void setAppend(boolean append) {
        isAppend = append;
    }

    public boolean isCloneSheetTemplate() {
        return isCloneSheetTemplate;
    }

    public void setCloneSheetTemplate(boolean cloneSheetTemplate) {
        isCloneSheetTemplate = cloneSheetTemplate;
    }

    public void addMergedRegion(ExcelRange excelRange) {
        List<ExcelRange> excelRanges = this.mergedRegions.get(excelRange.getSheetNum());
        if (excelRanges == null) {
            excelRanges = new ArrayList<>();
            this.mergedRegions.put(excelRange.getSheetNum(), excelRanges);
        }
        excelRanges.add(excelRange);
    }

    public void addFooter(ExcelRange excelRange) {
        this.footers.put(excelRange.getSheetNum(), excelRange);
    }

    public void addFooter(int sheetNum, int beginRowNum, int endRowNum) {
        ExcelRange footer = ExcelRange.builder()
                .sheetNum(sheetNum)
                .beginRowNum(beginRowNum)
                .endRowNum(endRowNum)
                .build();
        this.addFooter(footer);
    }

    public ExcelRange getFooter(int sheetNum) {
        return this.footers.get(sheetNum);
    }


    public List<ExcelRange> getMergedRegions(int sheetNum) {
        return this.mergedRegions.get(sheetNum);
    }

    public void setExcelCellWriter(ExcelCellWriter excelCellWriter) {
        this.excelCellWriter = excelCellWriter;
    }

    public void setExcelRowWriter(ExcelRowWriter excelRowWriter) {
        this.excelRowWriter = excelRowWriter;
    }

    public void setExcelBookWriter(ExcelBookWriter excelBookWriter) {
        this.excelBookWriter = excelBookWriter;
    }


    public void setExcelSheetWriter(ExcelSheetWriter excelSheetWriter) {
        this.excelSheetWriter = excelSheetWriter;
    }

    public ExcelVersion getVersion() {
        return version;
    }

    public void setVersion(ExcelVersion version) {
        this.version = version;
    }

    public ExcelWriteProcessor getExcelWriteProcessor() {
        return excelWriteProcessor;
    }

    public void setExcelWriteProcessor(ExcelWriteProcessor excelWriteProcessor) {
        this.excelWriteProcessor = excelWriteProcessor;
    }

    public Map<Integer, List<ExcelRange>> getMergedRegions() {
        return mergedRegions;
    }
}
